<template>
  <div id="versionEdit">
    <el-dialog title="编辑系统版本" size="tiny" :visible.sync="dialogShow" :before-close="cancelEdit" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="客户端" :label-width="formLabelWidth">
          {{form.channelText}}
        </el-form-item>
        <el-form-item label="操作系统" :label-width="formLabelWidth">
          {{form.phoneType}}
        </el-form-item>
        <el-form-item label="版本号" :label-width="formLabelWidth" prop="version">
          <el-input v-model="form.version"></el-input>
        </el-form-item>
        <el-form-item label="标题" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="强制更新" :label-width="formLabelWidth">
           <el-radio class="radio" v-model="form.isFocusUpdate" label="YES">强制更新</el-radio>
           <el-radio class="radio" v-model="form.isFocusUpdate" label="NO">不强制更新</el-radio>
        </el-form-item>
        <el-form-item label="版本范围" :label-width="formLabelWidth"  v-if="form.isFocusUpdate === 'YES'">
          <el-col :span="8">
            <el-input v-model="form.focusUpdateMin"></el-input>
          </el-col>
          <el-col :span="2" style="text-align:center">
            ~
          </el-col>
          <el-col :span="8">
            <el-input v-model="form.focusUpdateMax"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="form.description">
            </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getVersionDetail, updateVersionDetail } from 'api/system'
import {formRules} from '../config'
import { postLog } from 'api/common/index.js'
export default {
  props: ['id'],
  data () {
    return {
      form: {
        name: '',
        version: '',
        isFocusUpdate: 'YES',
        channel: '',
        phoneType: '',
        description: '',
        focusUpdateMin: '',
        focusUpdateMax: ''
      },
      formLabelWidth: '5rem',
      rules: {},
      dialogShow: true
    }
  },
  created () {
    this.rules = {...formRules}
    this.getVersionDetail();
  },
  watch: {
    id (val, oldval) {
      if (val !== '') {
        this.getVersionDetail();
      }
    }
  },
  methods: {
    cancelEdit () {
      this.$emit('closeDialog');
    },
    submit () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.update();
        } else {
          this.$alert('请按要求输入内容', '提示', {
            confirmButtonText: '确定'
          })
          return false;
        }
      })
    },
    async update () {
      if (this.form.isFocusUpdate === 'NO') {
        this.form.focusUpdateMin = '';
        this.form.focusUpdateMax = '';
      } else {
        if (!this.form.focusUpdateMin || !this.form.focusUpdateMax) {
          this.$alert('请输入版本范围', '提示');
          return false;
        }
      }
      try {
        let params = {
          channel: this.form.channel,
          phoneType: this.form.phoneType,
          name: this.form.name,
          description: this.form.description,
          isFocusUpdate: this.form.isFocusUpdate,
          appVersion: this.form.version,               //后台字段不统一,后台存为version
          focusUpdateMin: this.form.focusUpdateMin,
          focusUpdateMax: this.form.focusUpdateMax,
          operator: sessionStorage.getItem('realName') || sessionStorage.getItem('USER_NAME')
        }
        let back = await updateVersionDetail(params);
        back = back.data
        if (back.resultCode === '200') {
          postLog('VERSION_MANAGEMENT_EDIT', this.id, params.name, params)
          this.$emit('updateList');
        } else {
          this.$alert(back.msg);
        }
      } catch (err) {
        this.$alert('请求出错', '提示');
      }
      this.cancelEdit();
    },
    async getVersionDetail () {
      if (this.id) {
        try {
          let params = {
            id: this.id
          }
          let back = await getVersionDetail(params);
          back = back.data;
          if (back.resultCode === '200') {
            this.form = {...back.result};
          } else {
            this.$alert(back.msg);
          }
        } catch (err) {
          this.$alert('请求出错', '提示');
        }
      }
    }
  }
}
</script>
<style lang="less">

</style>

